package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.*;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 删除商品库存
     */
    @GetMapping("/goods/delelte/{id}/{num}")
    public ResponseEntity<Boolean> delelteStockBySkuId(@PathVariable("id")Long id,@PathVariable("num")Integer num){
        boolean flag = this.goodsService.deleteStocckBySkuId(id,num);
        return  ResponseEntity.ok(flag);
    }


    /**
     * 查询spu数据
     * @param page
     * @param rows
     * @param saleable
     * @param key
     * @param desc
     * @param sortBy
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc,
            @RequestParam(value = "sortBy", required = false) String sortBy){
        PageResult<SpuBo> result = goodsService.querySpuByPage(page,rows,saleable,key,sortBy,desc);
        if(CollectionUtils.isEmpty(result.getItems())){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增商品
     * @param spuBo
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
        try{
            goodsService.saveGoods(spuBo);
            return new ResponseEntity<>(HttpStatus.CREATED);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 对商品数据进行修改操作
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        try{
            goodsService.updateGoods(spuBo);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 通过spuId获得商品详情
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){
        SpuDetail spuDetail=goodsService.querySpuDetailBySpuId(spuId);
        if(spuDetail==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 通过spuId获得skus(集合)
     * @param spuId
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id") Long spuId){
        List<Sku> skus=goodsService.querySkusBySpuId(spuId);
        if(CollectionUtils.isEmpty(skus)){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * 根据id删除商品数据
     * @param id
     * @return
     */
    @DeleteMapping("goods/delete/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("id") Long id){
        try{
            goodsService.deleteGoods(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 对商品进行上下架的操作
     * @param spu
     * @return
     */
    @PostMapping("goods/upperAndLowerShelves")
    public ResponseEntity<Void> upperAndLowerShelves(@RequestBody Spu spu){
        try{
            goodsService.upperAndLowerShelves(spu);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        Spu spu=goodsService.querySpuById(id);
        if(spu==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spu);
    }

    @GetMapping("sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id") Long id){
        Sku sku=goodsService.querySkuById(id);
        if(sku==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(sku);
    }

    @GetMapping("saleable")
    public ResponseEntity<Cart> querySaleable(@RequestParam("skuId") Long skuId) {

        Cart cart=this.goodsService.querySaleable(skuId);

        if (cart == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }

        return ResponseEntity.ok(cart);
    }

}
